exports.contentDetail = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};

    var m_browse_count = 0;

    var contentData = 0;

    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    //任务流创建
    async.waterfall([

        //获取文章详情
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_content + "/fc_id/" + restfulParams.fc_id + "/skip/" + skip + "/limit/" + limit,
                    method: 'GET'

                },
                function (err, data) {

                    resultData.contentData = data.data;
                    contentData = data.data;
                    cb();
                }
            );
        },

        //判断文章的发送者类型
        function (cb) {
            if (contentData.length > 0) {
                var ucType = resultData.contentData[0]['fc_utype'];
                cb();
            } else {
                cb();
            }
        },

        function (cb) {
            if (contentData.length > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchCategory + "/fc_id/" + contentData[0].fc_fcid,
                        method: 'POST',
                        data: {
                            fields: ["fc_name"].join(",")
                        }
                    },
                    function (err, data) {
                        if (err) {
                            return RES.response(res, false, err);
                        }
                        var categoryData = data.data;
                        resultData.fc_name = categoryData[0].fc_name;

                        cb();
                    }
                );
            } else {
                cb();
            }
        },

        //获取文章评论
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_comment + "/fcc_fcid/" + restfulParams.fc_id + "/fcc_fid/0",
                    method: 'POST',
                    data: {
                        fields: [
                            "fcc_id",
                            "fcc_fcid",
                            "fcc_content",
                            "fcc_uid",
                            "fcc_reply_uid",
                            "fcc_is_reply",
                            "fcc_zan_count",
                            "fcc_is_author_commnet",
                            "fcc_create_time",
                            "fcc_fid",
                            "u_nickname",
                            "u_type",
                            "u_header_url"
                        ].join(",")
                    }

                },
                function (err, data) {

                    //console.log(data);

                    resultData.commentData = data.data;

                    cb();
                }
            );
        },
        //查询当前用户是否对文章评论点赞
        function (cb) {

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.commentData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //查询对该用户的回复
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchContentCommentZanRecord + "/is_delete/0/fcczr_fcid/" + item.fcc_id + "/fcczr_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ["fcczr_id"].join(",")
                            }
                        },
                        function (err, data) {

                            var is_exist = data.data;
// //console.log(is_exist);
                            if (is_exist.length <= 0) {
                                item.isZan = 0;
                            } else {
                                item.isZan = 1;
                            }
                            cb();
                        });
                });
            });
            async.waterfall(tasks, cb);

        },
        //查询该用户下的评论
        function (cb) {

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.commentData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //查询对该用户的回复
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_comment + "/fcc_fid/" + item.fcc_id + "/count/1",
                            method: 'POST',
                            data: {
                                fields: [
                                    "fcc_id",
                                    "fcc_content",
                                    "u_nickname"
                                ].join(",")
                            }

                        },
                        function (err, data) {

                            item.replyCount = data.count;
                            item.replyData = data.data;

                            cb();
                        });

                });

            });

            async.waterfall(tasks, cb);

        },
        //查询当前用户是否点过赞
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchContentZanRecord + "/is_delete/0/fczr_fcid/" + restfulParams.fc_id + "/fczr_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['fczr_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan <= 0) {
                            resultData.isZanData = 0;
                        } else {
                            resultData.isZanData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },

        //查询当前用户是否已收藏该回答
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/2/uc_fid/" + restfulParams.fc_id + "/uc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['uc_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan.length <= 0) {
                            resultData.isCollectData = 0;
                        } else {
                            resultData.isCollectData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },
        //浏览数加1
        function (cb) {
            if (contentData.length > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.update_fc_browse_count,
                        method: 'POST',
                        data: {
                            fc_id: resultData.contentData[0].fc_id,
                            fc_browse_count: parseInt(resultData.contentData[0].fc_browse_count) + 1
                        }

                    },

                    function (err, data) {
                        //console.log(data);
                        if (err) {
                            return RES.response(res, false, data);
                        }
                        resultData.isaddone = data;
                        cb();
                    }
                );
            } else {
                cb();
            }

        },
        //查询达人信息
        function (cb) {
            if (contentData.length > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_master + "/m_uid/" + resultData.contentData[0]['fc_uid'],
                        method: 'POST',
                        data: {
                            fields: [
                                "m_browse_count"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        m_browse_count = data.data;
                        cb();
                    }
                );
            } else {
                cb();
            }

        },
        //更新达人的浏览量
        function (cb) {
            if (m_browse_count.length > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.update_master,
                        method: 'POST',
                        data: {
                            m_uid: resultData.contentData[0].fc_uid,
                            m_browse_count: parseInt(m_browse_count[0].m_browse_count) + 1
                        }

                    },

                    function (err, data) {
                        //console.log(data);
                        if (err) {
                            return RES.response(res, false, data);
                        }
                        resultData.isMasterAddone = data;
                        cb();
                    }
                );
            } else {
                cb();
            }
        },
        //查询当前用户关注达人的状态
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchMasterConcern + "/is_delete/0/mc_master_id/" + restfulParams.m_uid + "/mc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['mc_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan.length <= 0) {
                            resultData.isConcern = 0;
                        } else {
                            resultData.isConcern = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },

    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, resultData);

    });

};